/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package app.actions;

import app.actionForms.AltaLibroValidatorForm;
import app.modelo.Libro;
import app.negocio.GestionLibreria;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Johann
 */
public class AltaLibroAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String KO = "ko";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        GestionLibreria gestionLibreria = new GestionLibreria();

        // 1.- Recuperamos los datos del validador del formulario.
        AltaLibroValidatorForm formulario = (AltaLibroValidatorForm) form;
        Libro libro = new Libro();
        libro.setAutor(formulario.getAutor());
        libro.setDescripcion(formulario.getDescripcion());
        libro.setEditorial(formulario.getEditorial());
        libro.setIsbn(formulario.getIsbn());
        libro.setPrecio(formulario.getPrecio());
        libro.setPublicacion(formulario.getPublicacion());
        libro.setTitulo(formulario.getTitulo());

        //2.- Invocamos a BD
        if (gestionLibreria.altaLibro(libro)) {
            request.setAttribute("libroAltaOK", "Libro insertado correctamente");
            request.setAttribute("libroAltaKO", null);
            // 3.- Redirigir a la vista elegida
            return mapping.findForward(SUCCESS);
        } else {
            request.setAttribute("libroAltaKO", "No se ha podido insertar el libro.");
            request.setAttribute("libroAltaOK", null);
            // 3.- Redirigir a la vista elegida
            return mapping.findForward(KO);
        }

    }
}
